<?php
declare (strict_types = 1);

namespace app\controller\api;

use app\BaseController;
use app\exception\BusinessException;
use app\service\shopify\action\graph\ProductGraph;
use app\service\shopify\action\rest\ProductRest;
use app\service\shopify\action\store\ProductsStore;
use think\helper\Arr;
use think\Request;

class ProductController extends BaseController
{

    /**
     * 显示指定的资源
     *
     * @param  string  $id
     * @return \think\Response
     */
    public function read(Request $request,$id)
    {
        //dd($id);
        $graph = new ProductGraph($request->middleware('x_shop_id'),true);
        $productRes = $graph->product($id);
        $data = [];
        $product = Arr::get($productRes,'data.product',[]);
        if(empty($product)) throw new BusinessException("goods not found");
        $variantList = Arr::get($product,'variants.nodes',[]);
        $data['product_id'] = $id;
        $data['product_title'] = Arr::get($product,'title','');
        $data['description'] = Arr::get($product,'description','');
        $data['descriptionHtml'] = Arr::get($product,'descriptionHtml','');
        $data['options'] = Arr::get($product,'options',[]);
        $media = Arr::get($product,'media.nodes',[]);
        $images = [];
        if($media){
            $images = array_map(function ($item) {
                return Arr::get($item,'preview.image.url','');
            },$media);
        }
        $data['images'] = $images;
        $variants = [];
        if($variantList){
            $variants = array_map(function ($item)use ($id){
                return [
                    'product_id'=>$id,
                    'variant_id'=>Arr::get($item,'id',''),
                    'variant_title'=>Arr::get($item,'title',''),
                    'price'=>Arr::get($item,'price',''),
                    'sku'=>Arr::get($item,'sku',''),
                    'image'=>Arr::get($item,'image.url',''),
                    'barcode'=>Arr::get($item,'barcode','')
                    ];
            },$variantList);
        }
        $data['variants'] = $variants;
        return $this->success($data);
    }

    public function index(Request $request)
    {
        $graph = new ProductGraph($request->middleware('x_shop_id'),true);
        $first = $request->get('first',1);
        $first = intval($first);
        $productsRes = $graph->getProductList($first);
        $data = [];
        $products = Arr::get($productsRes,'data.products.nodes',$data);
        if(empty($products)) return $this->success([]);
        $data = array_map(function ($item) {
            $medias = Arr::get($item,'media.nodes',[]);
            $media = Arr::first($medias);
            $preview = Arr::get($media,'preview.image.url','');
            $priceRangeV2 = Arr::get($item,'priceRangeV2',[]);
            $maxVariantPrice = Arr::get($priceRangeV2,'maxVariantPrice.amount','0.00');
            $minVariantPrice = Arr::get($priceRangeV2,'minVariantPrice.amount','0.00');
            $currencyCode = Arr::get($priceRangeV2,'minVariantPrice.currencyCode','');
            return [
                'product_id'=>Arr::get($item,'id',''),
                'title'=>Arr::get($item,'title',''),
                'image'=>$preview,
                'currencyCode'=>$currencyCode,
                'priceRange'=>sprintf("%s ~ %s",$minVariantPrice,$maxVariantPrice),

            ];
        },$products);

        return $this->success($data);
    }


}
